package cn.sunxyz.base.component;

/**
 * Created by yangrd on 2017/6/28.
 */
public class Sub {

    //求补码
    private static boolean[] complement(boolean[] bs) {
        int len = bs.length;
        //取反
        boolean[] bs1 = new boolean[len];
        //+1
        boolean[] bs2 = new boolean[len];
        for (int i = 0; i < len; i++) {
            bs1[i] = !bs[i];
            bs2[i] = false;
        }
        bs2[len - 1] = true;//00..1
        return Adder.add(bs1, bs2);
    }

    public static boolean[] sub(boolean[] bs1, boolean[] bs2) {
        boolean[] bs3 = complement(bs2);
        return Adder.add(bs1, bs3);
    }

    public static void main(String[] args) {
        boolean[] bs1 = {false, true, true, true};//0111
        boolean[] bs2 = {false, false, false, true};//001
        // 111-001 = 110 = 4+2+0=6
        System.out.println(Adder.boolToStr(sub(bs1, bs2)));
    }
}
